package pl.nazaweb.utils.restpojomaper;

import java.util.List;

/**
 *
 * @author naza - Marek Kawczyński
 */
public abstract class PojoAutoMapper<T> extends ReflectionHelper {

    protected abstract Object getAll();

    protected abstract Object create(List entities);
    private MapperDispatcher dispatcher;

    public PojoAutoMapper() {
        dispatcher = new MapperDispatcher(this);
    }

    public Object getAllEntities() {
        if (hasMethodHaveMapAsPojoAnnotation("getAll")) {
            return getDispatcher().pojoMapper(getAll(), "getAll");
        }
        if (hasMethodHaveMapAsMapAnnotation("getAll")) {
            return getDispatcher().mapMapper(getAll(), "getAll");
        }
        return getAll();
    }

    public Object createEntities(List<T> entitiesToCreate) {
        if (hasMethodHaveMapAsPojoAnnotation("create")) {
            return getDispatcher().pojoMapper(create(entitiesToCreate), "create");
        }
        if (hasMethodHaveMapAsMapAnnotation("create")) {
            return getDispatcher().mapMapper(create(entitiesToCreate), "create");
        }
        return create(entitiesToCreate);
    }

    public MapperDispatcher getDispatcher() {
        return dispatcher;
    }

    public void setDispatcher(MapperDispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }
}
